package com.zzj.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzj.entity.User;
import com.zzj.mapper.UserMapper;
import com.zzj.service.IUserService;
import com.zzj.utils.JWTUtil;
import com.zzj.utils.RSAUtil;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzj
 * @since 2023-05-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public boolean isSuperAdmin(String token) {
        String username = JWTUtil.getUsername(token);
        User user = this.findUserByUsername(username);
        if (user == null) {
            return false;
        }
        return user.getStatus() == 0;
    }

    @Override
    public boolean isAdmin(String token) {
        String username = JWTUtil.getUsername(token);
        User user = this.findUserByUsername(username);
        if (user == null) {
            return false;
        }
        return user.getStatus() == 5 || user.getStatus() == 0;
    }

    @Override
    public User findUserByUsername(String username) {
        return this.getOne(Wrappers.<User>lambdaQuery()
                        .eq(User::getUsername, username),
                false);
    }

    @Override
    public User findUserByUsernamePassword(String username, String password) throws Exception {
        password = RSAUtil.decrypted(password);
        password = SecureUtil.md5(password);
        return this.getOne(Wrappers.<User>lambdaQuery()
                        .eq(User::getUsername, username)
                        .eq(User::getPassword, password),
                false);
    }

    @Override
    public User findUserByEmail(String email) {
        return this.getOne(Wrappers.<User>lambdaQuery()
                        .eq(User::getEmail, email),
                false);
    }

    @Override
    public boolean setUserStatus(String username, int status) {
        return this.lambdaUpdate()
                .eq(User::getUsername, username)
                .set(User::getStatus, status).update();
    }

    @Override
    public int updateAvatar(String oldAvatar, String newAvatar) {
        return this.getBaseMapper().updateAvatar(oldAvatar, newAvatar);
    }

    @Override
    public int updateUsername(String oldUsername, String newUsername) {
        return this.getBaseMapper().updateUsername(oldUsername, newUsername);
    }

    @Override
    public boolean setUserEmail(String username, String email) {
        return this.lambdaUpdate()
                .eq(User::getUsername, username)
                .set(User::getEmail, email).update();
    }

    @Override
    public boolean setUserPassword(String username, String password) throws Exception {
        password=RSAUtil.decrypted(password);
        password = SecureUtil.md5(password);

        return this.lambdaUpdate()
                .eq(User::getUsername, username)
                .set(User::getPassword, password).update();
    }
}
